;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Sales.GiftExchange", [])
        .controller("GiftExchangeCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$timeout", "EventBus", "$filter", "UserContextService", "$stateParams", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $timeout, EventBus, $filter, UserContextService, $stateParams) {
            $scope.activeFlag = [true, false];
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.submitted = false;
            $scope.toolbarConfig = {
                showAdvanceSearch: false,
                showColConfig: false,
                showMateriel: false
            }
            $scope.orderObj = {};
            $scope.detailObj = {};
            $scope.tabFlag = {
                0: "详情",
                1: "新增"
            };

            $scope.changeTab = function (index) {
                $scope.activeFlag = [false, false, false];
                $scope.activeFlag[index] = true;
                $scope.submitted = false;
                switch (index) {
                    case 0:
                        break;
                    case 1:
                        break;
                    default:
                        break;
                }
            };

            /* 根据手机号和兑换码获取验证码 */
            $scope.getCode = function (ticketCode, phoneNo) {
                var service = "device";
                var reqUrl = "/giftticket/verifyCode";
                var param = {};
                if (!!ticketCode && !!phoneNo) {
                    param.ticketCode = ticketCode;
                    param.phoneNo = phoneNo;
                    return ybHttp.Get(service, reqUrl, param).then(function (resp) {
                        if (resp.code !== 0) {
                            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                            return;
                        }
                        ybWidget.ShowToaster('success', '验证码已发送至手机，请注意查收');
                    });
                } else {
                    ybWidget.ShowToaster('warning', '请正确输入兑换码和手机号！');
                }
            };


            /* 获取兑换详情列表 */
            $scope.saveData = function () {
                if (!$scope.orderObj.ticketCode || !$scope.orderObj.phoneNo || !$scope.orderObj.verifyCode) {
                    ybWidget.ShowToaster('warning', '请正确填写页面信息！');
                    $scope.submitted = true;
                    return;
                }
                var service = "device";
                var reqUrl = "/giftticket/detailInfo";
                var param = angular.copy($scope.orderObj);
                return ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    $scope.changeTab(1);
                    $scope.detailObj = resp.value;
                    $scope.detailObj.validityDate = $filter("YbDate")(resp.value.validityDate, 'yyyy-MM-dd HH:mm:ss');
                });
            }

            $scope.doExchange = function () {
                ybWidget.ShowMessage("确定要兑换吗？", MESG_TYPE.Ask).then(function () {
                    window.sessionStorage.setItem('giftItem', JSON.stringify($scope.detailObj));
                    $state.go("app.sales.giftOrder");
                })
            };

            $scope.cancel = function () {
                $scope.changeTab(0);
            };
        }]);
})(window, window.angular);
